package com.common.base.tools.utils

import com.common.base.http.main.ApiConfig
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.formatter.border.DefaultBorderFormatter
import com.elvishew.xlog.formatter.message.json.DefaultJsonFormatter
import com.elvishew.xlog.formatter.message.throwable.DefaultThrowableFormatter
import com.elvishew.xlog.formatter.message.xml.DefaultXmlFormatter
import com.elvishew.xlog.formatter.stacktrace.DefaultStackTraceFormatter
import com.elvishew.xlog.formatter.thread.DefaultThreadFormatter
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.NeverBackupStrategy
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator


/**
 * Created by zsf on 2021/1/11 14:25
 * ******************************************
 * * 自定义日志
 * ******************************************
 */

var logger = Logs

object Logs {

    fun initXLog() {
        val config = LogConfiguration.Builder()
            .logLevel(
                if (ApiConfig.isPrintHttpLog) {
                    LogLevel.ALL
                } else {
                    LogLevel.NONE
                }
            )
            .tag(ApiConfig.HTTP_TAG) // Specify TAG, default: "X-LOG"
//            .enableThreadInfo() // Enable thread info, disabled by default
//            .enableStackTrace(2) // Enable stack trace info with depth 2, disabled by default
//            .enableBorder() // Enable border, disabled by default
            .jsonFormatter(DefaultJsonFormatter()) // Default: DefaultJsonFormatter
            .xmlFormatter(DefaultXmlFormatter()) // Default: DefaultXmlFormatter
            .throwableFormatter(DefaultThrowableFormatter()) // Default: DefaultThrowableFormatter
            .threadFormatter(DefaultThreadFormatter()) // Default: DefaultThreadFormatter
            .stackTraceFormatter(DefaultStackTraceFormatter()) // Default: DefaultStackTraceFormatter
            .borderFormatter(DefaultBorderFormatter()) // Default: DefaultBorderFormatter
//            .addObjectFormatter(
//                AnyClass::class.java,  // Add formatter for specific class of object
//                AnyClassObjectFormatter()
//            ) // Use Object.toString() by default
//            .addInterceptor(
//                BlacklistTagsFilterInterceptor( // Add blacklist tags filter
//                    "blacklist1", "blacklist2", "blacklist3"
//                )
//            )
//            .addInterceptor(MyInterceptor()) // Add other log interceptor
            .build()

        val androidPrinter: com.elvishew.xlog.printer.Printer =
            AndroidPrinter(true) // Printer that print the log using android.util.Log

//        val consolePrinter: Printer =
//            ConsolePrinter() // Printer that print the log to console using System.out

        val filePrinter: com.elvishew.xlog.printer.Printer =
            FilePrinter.Builder(ApiConfig.log_path) // Specify the directory path of log file(s)
                .fileNameGenerator(DateFileNameGenerator()) // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(NeverBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024)
                .cleanStrategy(FileLastModifiedCleanStrategy(ApiConfig.log_size)) // Default: NeverCleanStrategy()
                .flattener(MyEncryptFlattener()) // Default: DefaultFlattener
//                .writer(SimpleWriter()) // Default: SimpleWriter
                .build()

        XLog.init( // Initialize XLog
            config,  // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
            androidPrinter,  // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
            filePrinter
        )
    }
    @JvmStatic
    fun info(message: String? = "null") {
        XLog.tag(ApiConfig.HTTP_TAG).i(message)
    }

    @JvmStatic
    fun info(any: Any?) {
        XLog.tag(ApiConfig.HTTP_TAG).i(JsonUtils.toJson(any))
    }
    @JvmStatic
    fun error(message: String? = "null") {
        XLog.tag(ApiConfig.HTTP_TAG).e(message)
    }
    @JvmStatic
    fun error(tr: Throwable? = Throwable()) {
        XLog.tag(ApiConfig.HTTP_TAG).e(tr)
    }
    @JvmStatic
    fun error(message: String? = "null", tr: Throwable? = Throwable()) {
        XLog.tag(ApiConfig.HTTP_TAG).e(message, tr)
    }


}









